package com.sdw.message.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.sdw.message.dao.base.DataBase;
import com.sdw.message.dao.base.DataReader;
import com.sdw.message.pojo.Reply;
import com.sdw.message.pojo.ReplyType;

@Repository("replyDao")
public class ReplyDao {

	@Resource
	private DataBase messageDatabase;
	
	public Iterable<Reply> getAllReplyByMessageId(long messageId){
		String sql = "select id, mid, cont, type, score, department, username, createtime, modifytime from [dbo].[sys_reply] "
				+ " where mid=" + messageId + " order by modifytime desc";
		DataReader reader = null;
		List<Reply> list = new ArrayList<Reply>();
		
		try{
			reader = this.messageDatabase.executeQuery(sql);
			while(reader.next()){
				list.add(this.conver(reader));
			}
			return list;
		}finally{
			if(reader != null){
				reader.close();
			}
		}
		
	}
	
	private Reply conver(DataReader reader){
		int index = 1;
		Reply reply = new Reply();
		
		reply.setId(reader.getLong(index++));
		reply.setMid(reader.getLong(index++));
		reply.setCont(reader.getString(index++));
		reply.setReplyType(ReplyType.valueOf(reader.getString(index++)));
		reply.setScore(reader.getByte(index++));
		reply.setDepartment(reader.getString(index++));
		reply.setUserName(reader.getString(index++));
		reply.setCreateTime(reader.getDate(index++));
		reply.setModifyTime(reader.getDate(index++));
		
		return reply;
	}
}
